//
//  UIViewExtension.swift
//  swiftWeGood
//
//  Created by DJ梁 on 2019/12/31.
//  Copyright © 2019 changdaokeji. All rights reserved.
//

import UIKit


let SCREENW =  UIScreen.main.bounds.size.width
let SCREENH = UIScreen.main.bounds.size.height
extension UIView{
    // MARK: - 常用位置属性
    public var AJLeft:CGFloat {
        get {
            return self.frame.origin.x
        }
        set(newLeft) {
            var frame = self.frame
            frame.origin.x = newLeft
            self.frame = frame
        }
    }
    
    public var AJTop:CGFloat {
        get {
            return self.frame.origin.y
        }
        
        set(newTop) {
            var frame = self.frame
            frame.origin.y = newTop
            self.frame = frame
        }
    }
    
    public var AJWidth:CGFloat {
        get {
            return self.frame.size.width
        }
        
        set(newWidth) {
            var frame = self.frame
            frame.size.width = newWidth
            self.frame = frame
        }
    }
    
    public var AJHeight:CGFloat {
        get {
            return self.frame.size.height
        }
        
        set(newHeight) {
            var frame = self.frame
            frame.size.height = newHeight
            self.frame = frame
        }
    }
    
    public var AJRight:CGFloat {
        get {
            return self.AJLeft + self.AJWidth
        }set(newRight) {
            var frame = self.frame
            frame.origin.x = newRight - frame.size.width
            self.frame = frame
        }
    }
    
    public var AJBottom:CGFloat {
        get {
            return self.AJTop + self.AJHeight
        }
        set(newBottom)
        {
            self.AJTop = newBottom - self.AJHeight;
        }
    }
    
    public var AJCenterX:CGFloat {
        get {
            return self.center.x
        }
        
        set(newCenterX) {
            var center = self.center
            center.x = newCenterX
            self.center = center
        }
    }
    
    public var AJCenterY:CGFloat {
        get {
            return self.center.y
        }
        
        set(newCenterY) {
            var center = self.center
            center.y = newCenterY
            self.center = center
        }
    }
    
    ///大小
    public var AJSize:CGSize{
        get{
            return self.bounds.size
        }
        set(newSize)
        {
            var rect = self.frame;
            rect.size = newSize;
            self.frame = rect;
        }
    }
    
    static func awake() {
//        let instance = UIView.appearance()
//        instance.backgroundColor = by_color_white
    }
    
    //获取父控制器
    public var BY_nextresponsder : UIViewController? {
        if self.next == nil {
            return nil
        }
        
        var vc:UIResponder = self
        while vc.isKind(of: UIViewController.self) != true {
            vc = vc.next!
        }
        return vc as? UIViewController
    }
    
    
    
}

// 设置渐变颜色的扩展
extension UIView {
    func setGradientLayer(colors:[CGColor],
                      startPoint:CGPoint = CGPoint(x: 0, y: 1),
                        endPoint: CGPoint = CGPoint(x: 0, y: 0),
                       locations: [NSNumber] = [0.0,1.0]) {
        let graduPic = CAGradientLayer()
        graduPic.startPoint = startPoint
        graduPic.endPoint = endPoint
        graduPic.locations = locations
        graduPic.colors = colors
        self.layer.addSublayer(graduPic)
    }
}

// MARK: 切圆角
extension UIView {
    func cornerRadius( of radius:CGFloat){
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }
    func cornerRadius( of radius:CGFloat,isClip:Bool = true){
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = isClip
    }
    func clipCorner(corners:UIRectCorner,radius:CGFloat){
        self.layoutIfNeeded()
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners:corners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
        
    }
}
